//
//  THBottomPushView.m
//  BusinessCloud
//
//  Created by xin on 2019/1/15.
//  Copyright © 2019 天九网络科技. All rights reserved.
//

#import "THBottomPushView.h"
#import "UIView+Additions.h"
#import "UIColor+String.h"
#import "UIControl+PKExtend.h"

@implementation THBottomPushView

- (instancetype)initWithCurrentVC:(UIViewController *)currentVC
{
    if (self = [super init]) {
        _currentVC = currentVC;
        [self setUI];
    }
    return self;
}

- (instancetype)init
{
    if (self = [super init]) {
        [self setUI];
    }
    return self;
}

- (void)setUI {
    
    _contentHeight = 293;
    
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundView.frame = self.bounds;
    
    self.shadowView.frame = CGRectMake(0, self.bounds.size.height,self.bounds.size.width, _contentHeight);
    self.contentView.frame = self.shadowView.bounds;
    [self addSubview:self.backgroundView];
    [self addSubview:self.shadowView];
    [self.shadowView addSubview:self.contentView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.closeButton];
    [self.contentView addSubview:self.doneButton];
    
    [self initSubViews];
    
    //contentView top left right圆角
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.contentView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(13,13)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = _contentView.bounds;
    maskLayer.path = maskPath.CGPath;
    _contentView.layer.mask = maskLayer;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.closeButton.frame = CGRectMake(16, 16, 20, 20);
    
    CGSize titleSize = [self.titleLabel sizeThatFits:CGSizeMake(self.bounds.size.width, 50)];
    
    self.titleLabel.frame = CGRectMake((self.bounds.size.width-titleSize.width)/2., 0, titleSize.width, 50);
    
    CGSize doneSize = [self.doneButton sizeThatFits:CGSizeMake(self.bounds.size.width, 30)];
    doneSize.width += 16;
    self.doneButton.size = doneSize;
    self.doneButton.centerY = self.closeButton.centerY;
    self.doneButton.x = self.width - doneSize.width - 10;
    
    self.shadowView.height = self.contentHeight;
    self.contentView.height = self.contentHeight;
}

#pragma mark - public

- (void)initSubViews
{
    
}

- (void)doneAction:(id)sender
{
    
}

#pragma mark - Anim
- (void)show
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    self.alpha = 1;
    [rootWindow addSubview:self];
    self.backgroundView.alpha = 0;
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:1 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.backgroundView.alpha = 1;
        CGRect contentViewRect = self.shadowView.frame;
        contentViewRect.origin.y = self.bounds.size.height - contentViewRect.size.height;
        self.shadowView.frame = contentViewRect;
    } completion:^(BOOL finished) {
    }];
}

- (void)showInView:(UIView *)view
{
    if (!view) {
        return;
    }
    
    self.alpha = 1;
    [view addSubview:self];
    self.backgroundView.alpha = 0;
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:1 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.backgroundView.alpha = 1;
        CGRect contentViewRect = self.shadowView.frame;
        contentViewRect.origin.y = self.bounds.size.height - contentViewRect.size.height;
        self.shadowView.frame = contentViewRect;
    } completion:^(BOOL finished) {
    }];
}

- (void)dismiss
{
    [self endEditing:YES];
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.alpha = 0;
        CGRect contentViewRect = self.shadowView.frame;
        contentViewRect.origin.y = self.bounds.size.height;
        self.shadowView.frame = contentViewRect;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


#pragma mark - private
//截屏
- (UIImage *)snapshotScreenInView:(UIView *)contentView {
    CGSize size = contentView.bounds.size;
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    CGRect rect = contentView.frame;
    [contentView drawViewHierarchyInRect:rect afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

#pragma mark - Action
- (void)closeAction:(id)sender
{
    [self dismiss];
}

- (void)sendAction:(id)sender
{
    if (self.rightButtonClickedBlock) {
        self.rightButtonClickedBlock(sender);
    }
    [self doneAction:sender];
    [self dismiss];
}

#pragma mark - Set
- (void)setTitle:(NSString *)title
{
    _title = title;
    
    _titleLabel.text = title;
}

- (void)setRightTitle:(NSString *)rightTitle {
    _rightTitle = rightTitle;
    [_doneButton setTitle:rightTitle forState:UIControlStateNormal];
}

- (void)setContentHeight:(CGFloat)contentHeight
{
    if (contentHeight != _contentHeight) {
        _contentHeight = contentHeight;
        [self setNeedsLayout];
        [self layoutIfNeeded];
    }
}

#pragma mark - Getter
- (UIVisualEffectView *)backgroundView
{
    if (!_backgroundView) {
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        _backgroundView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
                [_backgroundView addGestureRecognizer:tap];
        }
    return _backgroundView;
}

- (UIView *)shadowView
{
    if(!_shadowView) {
        _shadowView = [[UIView alloc] init];
        _shadowView.layer.masksToBounds = NO;//默认是NO
        _shadowView.layer.shadowOffset = CGSizeMake(0, 0);
        _shadowView.layer.shadowRadius = 23;
        _shadowView.layer.shadowOpacity = 1;
        _shadowView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1].CGColor;
    }
    return _shadowView;
}

- (UIView *)contentView
{
    if(!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor whiteColor];
    }
    return _contentView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textColor = [UIColor colorWithString:@"#333333"];
    }
    return _titleLabel;
}

- (UIButton *)closeButton
{
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
        [_closeButton setImage:[UIImage imageNamed:@"input_close"] forState:UIControlStateNormal];
        _closeButton.pk_enlargeTouchInsets = UIEdgeInsetsMake(20, 20, 10, 20);
    }
    return _closeButton;
}

- (UIButton *)doneButton
{
    if (!_doneButton) {
        _doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_doneButton addTarget:self action:@selector(sendAction:) forControlEvents:UIControlEventTouchUpInside];
        _doneButton.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [_doneButton setTitle:@"确认" forState:UIControlStateNormal];
        [_doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return _doneButton;
}


@end
